package com.lanou.musicbox.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.EnterTheSongsAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.EnterTheSongs;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.List;

/**
 * Created by 赵宝龙 on 15/10/22.
 * 进入歌单页面
 */
public class EnterTheSongsFragment extends BaseFragment {
    private  Toolbar toolbar;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private ListView entertheListView;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private EnterTheSongs data;
    private List<EnterTheSongs.ContentEntity> datas;
    private EnterTheSongsAdapter adapter;
    private String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.diy.gedanInfo&format=json&listid=";
    private String songs_id;
    private ImageView pic;
    private TextView title, tag, listenum, number, desc;
    private String url_ALL;
    public EnterTheSongsFragment(){
        super();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.enterthe_song, null);
        entertheListView = (ListView) v.findViewById(R.id.entertheListView);

        pic = (ImageView) v.findViewById(R.id.pic);
        title = (TextView) v.findViewById(R.id.title);
        tag = (TextView) v.findViewById(R.id.tag);
        listenum = (TextView) v.findViewById(R.id.listenum);
        number = (TextView) v.findViewById(R.id.number);
        desc = (TextView) v.findViewById(R.id.desc);
        toolbar = (Toolbar)v.findViewById(R.id.toolbar);
        collapsingToolbarLayout = (CollapsingToolbarLayout)v.findViewById(R.id.collapsing_toolbar);

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        toolbar.setNavigationIcon(R.mipmap.back_btn);
        collapsingToolbarLayout.setTitleEnabled(true);

        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
        imageLoader = volleySingleton.getImageLoader();

        Bundle bundle = getArguments();
        songs_id = bundle.getString("songs_id");

        url_ALL = url+songs_id;

        Log.i("lanou111", "onCreateView_url_ALL " + url_ALL);
        initData();// 定义一个用来解析的方法
        //接收值
    }



    private void initData() {
        StringRequest request = new StringRequest(url_ALL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                data = gson.fromJson(response, EnterTheSongs.class);
                datas = data.getContent();
                Log.e("ZBL","!!!!!!!!!!!!!!!!!!!!!" + response);
                adapter = new EnterTheSongsAdapter(data, getActivity());
                initView();
                entertheListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }
    private void initView() {
       // Log.e("ZBL", "######################" + data.getTitle());
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(pic, R.mipmap.lmbsn, R.mipmap.lmbsn);
        imageLoader.get(data.getPic_500(), listener);
       // title.setText(data.getTitle());
        tag.setText(data.getTag());
        listenum.setText(data.getListenum());
        collapsingToolbarLayout.setTitle(data.getTitle());
      //  Log.e("ZBL", "%%%%%%%%%%%%%%%%%%%" + datas.size());
        if (datas != null){
            number.setText(String.valueOf(datas.size()));
        }
        desc.setText(data.getDesc());
    }

}
